<!--回弹业务数据字典---回弹仪器设备管理---查看-->
<template>
  <div class="department" id="department">
    <!-- 右侧 -->
    <div class="department_right">
      <!-- 顶部导航栏 -->
      <div class="mechanism_nav">
        <!-- 左侧 -->
        <div class="mechanism_nav_left">
          <el-radio-group
            v-model="radio"
            size="small"
            @change="modelType($event)"
          >
            <el-radio-button label="待办"></el-radio-button>
            <el-radio-button label="已办"></el-radio-button>
            <!-- #2b5a9d -->
          </el-radio-group>
        </div>
        <!-- 顶部按钮 -->
        <div class="print">
          <div class="printTable">
            <el-button
              type="primary"
              icon="el-icon-refresh"
              size="small"
              @click="yj_searchForm(getList)"
            ></el-button>
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="small"
              v-if="getList.modelType == '0' && yj_edit == true"
              @click="edit()"
              >编辑</el-button
            >
            <el-button
              type="primary"
              icon="el-icon-circle-check"
              size="small"
              v-if="
                getList.modelType == '0' &&
                yj_submit == true &&
                row.fStatus == '编辑中'
              "
              @click="SubmitText(fId)"
              >提交</el-button
            >
            <el-button
              type="primary"
              icon="el-icon-delete"
              size="small"
              v-if="
                getList.modelType == '0' &&
                yj_delete == true &&
                row.fStatus == '编辑中'
              "
              @click="rowDelete(fId)"
              >删除</el-button
            >
          </div>
        </div>
      </div>

      <!-- 表格 -->
      <div class="text_table">
        <tableData
          height="100%"
          :config="table_config"
          :dataList="tableData"
          :loading="loading"
          @selection-change="selectionChange"
          @sort-change="sortChange"
          @click-change="rowChange"
          @ClickOnLine="yj_viewForm"
        />
      </div>
      <!--配合比通知单编号弹框 查看 -->
      <div class="Control-1 mechanism_dialogS closure">
        <el-dialog
          :title="dialogTitle"
          :modal-append-to-body="false"
          v-dialogDrag
          :visible.sync="dialogEditVisible"
          v-loading="loadingEditDialog"
          element-loading-text="拼命加载中"
          element-loading-spinner="el-icon-loading"
          element-loading-background="rgba(0, 0, 0, 0.2)"
        >
          <div
            class="dialogBody common"
            :style="{ height: '650px' }"
            style="overflow: hidden"
          >
            <div style="overflow-y: auto; margin-bottom: 20px">
              <div style="height: 160px">
                <publicForm
                  ref="publicForm0"
                  lableWidth="95px"
                  :formObj="formObjText1"
                  :formData="formData"
                  :timestamp="formData.fScanfile"
                  :fileList="fSourcefileList"
                  @delteFile="delteFile"
                >
                </publicForm>
              </div>

              <div style="height: 50px"></div>
            </div>
          </div>
          <div class="newlyAdded_btn" v-if="btnShow">
            <el-button type="primary" @click="yj_saveForm()">保存</el-button>
            <el-button type="warning" @click="dialogEditVisible = false"
              >关闭</el-button
            >
          </div>
        </el-dialog>
      </div>

      <!-- 查看弹框 -->
      <div class="Control-1 mechanism_dialogS closure">
        <el-dialog
          :title="dialogTitle"
          :modal-append-to-body="false"
          v-dialogDrag
          :visible.sync="dialogEditVisible_dialog"
          v-loading="loadingEditDialog_dialog"
          element-loading-text="拼命加载中"
          element-loading-spinner="el-icon-loading"
          element-loading-background="rgba(0, 0, 0, 0.2)"
        >
          <div
            class="dialogBody common"
            :style="{ height: '650px' }"
            style="overflow: hidden"
          >
            <div style="margin-bottom: 5px; height: 100%">
              <div class="commonBigBox">
                <div class="commonBox" style="border-right: 1px solid #ccc">
                  <tableData
                    :config="table_config_dialog"
                    :dataList="tableData_dialog"
                    :loading="loading"
                    @selection-change="selectionChange"
                    @sort-change="sortChange"
                    @click-change="rowChange_dialog"
                    @ClickOnLine="yj_viewForm_dialog"
                  />
                </div>
                <div class="commonBox">
                  <publicForm
                    ref="publicForm0"
                    lableWidth="95px"
                    :formObj="formObjText_dialog"
                    :formData="row_dialog"
                    :timestamp="row_dialog.fScanfile"
                    :fileList="fSourcefileList_dialog"
                  >
                  </publicForm>
                </div>
              </div>

              <div style="height: 50px"></div>
            </div>
          </div>
        </el-dialog>
      </div>
    </div>
  </div>
</template>

<script>
import {
  getMachineCheckByFid, //编辑详情
  getMachineCheckListByFid, //查看详情
  saveMachineCheck, //保存
  getMachineCheckLists, //列表
  delMachineCheck, //删除
  submitMachineCheck, //提交
} from "@/api/request.js";
import TableData from "@/components/HntManage/el-tables"; //主页面列表
import publicForm from "@/components/public/el-form.vue"; //弹框form
import formJs from "./regexDetail.js"; //新增弹框配置项

export default {
  inject: ["reload"],
  name: "EquipmentText",
  data() {
    return {
      name: "EquipmentText",
      row: { fStatus: "" },

      yj_edit: null,
      yj_delete: null,
      yj_submit: null,
      radio: "待办",
      // 列表
      getList: {
        userId: "", // 查询待办或已办时传 列表页查询不传
        stationType: "2", //拌合机类型 1 - 砼拌合站 2-水稳拌合站 3-沥青拌合站
        companyId: "", // 当前登录用户标段id
        modelType: "0", // 0代办 1已办 2表示列表页查询
        stationId: "",
        machineId: "",
      },
      fId: null, //当前行数据fId
      loading: false, //列表转载
      table_config: formJs.table_configText,
      tableData: [], //表格列表数据列表
      // 列表

      // 弹框
      table_config_dialog: formJs.table_config_dialog,
      tableData_dialog: [],
      row_dialog: formJs.row_dialog,
      fId_dialog: null, //当前行数据fId
      loadingEditDialog_dialog: false, //弹框loading
      dialogEditVisible_dialog: false, //新增弹框是否显示
      fSourcefileList_dialog: [],
      formObjText_dialog: formJs.formObjText_dialog,


      timeYMD: null, //当前日期
      btnShow: true,
      dialogTitle: "",
      loadingEditDialog: false, //弹框loading
      dialogEditVisible: false, //新增弹框是否显示
      formObjText1: null,
      formObj1: formJs.formObj1, //信息编辑配置参数
      formData: formJs.formData,
      recordfiles: [], //删除文件
      fSourcefileList: [],
      saveData: null, //保存接口传的参数
      // 弹框

      userInfo: {}, //登录用户
    };
  },
  components: {
    TableData, //主页面列表
    publicForm, //表单
  },
  created() {},
  mounted() {
    this.userInfo = JSON.parse(sessionStorage.getItem("userInfo"));
    console.log(this.userInfo);
    this.getList = {
      stationType: "2", //拌合机类型 1 - 砼拌合站 2-水稳拌合站 3-沥青拌合站
      modelType: "0", // 0代办 1已办 2表示列表页查询
      companyId: this.userInfo.companyId, // 左侧选中组织机构id
      userId: this.userInfo.userId, // 查询待办或已办时传 列表页查询不传
      stationId: "",
      machineId: "",
    };
    this.getHeight();
    this.getData();
    this.yj_searchForm(this.getList);
    this.btnRole(this.name);
  },
  methods: {
    btnRole(name) {
      var btnList = [];
      var ModuleList = JSON.parse(sessionStorage.getItem("ModuleList"));
      if (ModuleList.length > 0) {
        ModuleList.map((item) => {
          if (item.fEncode == name) {
            btnList = item.modulebuttonList;
          }
        });
        var btnStr = [];
        btnList.map((item) => {
          btnStr.push(item.fEncode);
        });

        this.yj_edit = btnStr.includes("yj_edit");
        this.yj_delete = btnStr.includes("yj_delete");
        this.yj_submit = btnStr.includes("yj_submit");
      } else {
        this.yj_edit = true;
        this.yj_delete = true;
        this.yj_submit = true;
      }
    },

    modelType(e) {
      this.fId = "";
      if (e == "待办") {
        this.getList.modelType = "0";
        this.getList.userId = this.userInfo.userId;
      } else {
        this.getList.modelType = "1";
        this.getList.userId = this.userInfo.userId;
      }
      this.yj_searchForm(this.getList);
    },

    // 获取列表数据
    yj_searchForm(data) {
      this.loading = true;
      getMachineCheckLists(data)
        .then((res) => {
          if (res.errcode == 0) {
            var that = this;
            that.loading = false;
            that.tableData = res.data.machineCheckLists;
          } else {
            this.$message.error("列表", res.msg);
            this.loading = false;
          }
        })
        .catch(() => {
          console.log("列表");
          this.loading = false;
        });
    },

    edit() {
      this.recordfiles = [];
      this.formObjText1 = this.formObj1;
      this.disabled = false;
      this.dialogTitle = "编辑";
      this.btnShow = true;
      if (this.fId != "" && this.fId != null) {
        this.loadingEditDialog = true; //弹框loading
        this.dialogEditVisible = true;
        this.getConcrete(this.fId);
      } else {
        return this.$message.error("请先选择需要编辑的数据");
      }
    },
   
    // 获取详情
    getConcrete(id) {
      getMachineCheckByFid({ fId: id })
        .then((res) => {
          this.loadingEditDialog = false; //弹框loading
          this.formData = res.data.machineCheck;
          this.formData.fMaintainername = this.userInfo.realName;
          this.formData.fMaintainerid = this.userInfo.userId;

          if (this.formData.fMaintenancedates == null) {
            this.formData.fMaintenancedates = this.timeYMD;
          }

          if (this.formData.fCertificatenumber == null) {
            this.formData.fCertificatenumber = "";
          }
          if (this.formData.fCheckresult == null) {
            this.formData.fCheckresult = "";
          }
          if (this.formData.fNextcheckdates == null) {
            this.formData.fNextcheckdates = "";
          }
          this.fSourcefileList = res.data.scanFileList; //证书扫描件
        })
        .catch((err) => {
          console.log(err);
        });
    },
    // 保存
    yj_saveForm() {
      if (this.$refs.publicForm0.submitForm()) {
        this.saveData = {
          fId: this.formData.fId,
          userId: this.formData.fMaintainerid,
          userName: this.formData.fMaintainername,
          fProjectname: this.formData.fProjectname,
          fProjectid: this.formData.fProjectid,
          fCompanyname: this.formData.fCompanyname,
          fCompanyid: this.formData.fCompanyid,
          fStationid: this.formData.fStationid, //拌合站编号
          fStationname: this.formData.fStationname, //拌合站
          fMachineid: this.formData.fMachineid, //拌合机编号
          fMachinename: this.formData.fMachinename, //拌合机
          fCertificatenumber: this.formData.fCertificatenumber, //检定证书编号
          fCheckresult: this.formData.fCheckresult, //检定校准结果
          fNextcheckdates: this.formData.fNextcheckdates, //下次检定时间
          fScanfile: this.formData.fScanfile, //证书扫描件资料id
          fMaintainerid: this.formData.fMaintainerid, //维护人id
          fMaintainername: this.formData.fMaintainername, //维护人
          fMaintenancedates: this.formData.fMaintenancedates, //维护时间
          recordfiles: this.recordfiles, // 删除文件id集合
        };
        saveMachineCheck(this.saveData)
          .then((res) => {
            if (res.errcode == 0) {
              this.yj_searchForm(this.getList);
              this.dialogEditVisible = false;
              this.$message.success("新增成功");
            } else {
              this.$message.error(res.msg);
              console.log("接口报错");
            }
          })
          .catch((error) => {
            console.log(error);
          });
      } else {
        console.log("error");
      }
    },

    // 提交
    SubmitText(id) {
      if (id != "" && id != null) {
        this.$confirm("是否继续提交?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        })
          .then(() => {
            this.loading = true;
            submitMachineCheck({
              fId: id,
              userId: this.userInfo.userId,
              userName: this.userInfo.realName,
            })
              .then((res) => {
                if (res.errcode == 0) {
                  this.loading = false;
                  this.yj_searchForm(this.getList);
                  this.$message.success("提交成功");
                } else {
                  this.$message.error("提交失败", res.msg);
                  this.loading = false;
                }
              })
              .catch(() => {
                console.log("提交失败");
                this.loading = false;
              });
          })
          .catch(() => {
            this.$message("取消提交");
          });
      } else {
        return this.$message.error("请先选择需要提交的数据");
      }
    },

    // 删除
    rowDelete(id) {
      if (id != "" && id != null) {
        this.$confirm("此操作将永久删除该数据, 是否继续?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        })
          .then(() => {
            this.loading = true;
            delMachineCheck({ fId: id })
              .then((res) => {
                if (res.errcode == 0) {
                  this.loading = false;
                  this.yj_searchForm(this.getList);
                  this.$message.success("删除成功");
                } else {
                  this.$message.error("删除失败", res.msg);
                  this.loading = false;
                }
              })
              .catch(() => {
                console.log("删除失败");
                this.loading = false;
              });
          })
          .catch(() => {
           this.$message("取消删除");
          });
      } else {
        return this.$message.error("请先选择需要删除的数据");
      }
    },

    // 获取页面容器高度
    getHeight() {
      var h = document.documentElement.clientHeight; //获取页面可见高度
      this.dialogHeight = h * 0.36 + "px";
    },
    // 点击查看
    yj_viewForm(row, label) {
      this.row = row;
      this.fId = row.fId;
      if (label == "检定/校准次数") {
        this.editText_dialog(row.fId);
      }
    },
    yj_viewForm_dialog(row, label) {
      console.log(label);
      this.row_dialog = row;
      this.fId_dialog = row.fId;
      this.fSourcefileList_dialog = row.scanFileList;
    },
    editText_dialog(id) {
      this.dialogTitle = "查看";
      if (id != "" && id != null) {
        this.loadingEditDialog_dialog = true; //弹框loading
        this.dialogEditVisible_dialog = true;
        getMachineCheckListByFid({ fId: id })
          .then((res) => {
            if (res.errcode == 0) {
              var that = this;
              that.loadingEditDialog_dialog = false;
              this.tableData_dialog = res.data.machineCheckList;

              if (this.tableData_dialog.length != 0) {
                this.row_dialog = this.tableData_dialog[0];
                this.fSourcefileList_dialog = this.row_dialog.scanFileList;
              }
            } else {
              this.$message.error("查看", res.msg);
              this.loadingEditDialog = false;
            }
          })
          .catch(() => {
            console.log("查看详情接口报错");
            this.loadingEditDialog = false;
          });
      } else {
        return this.$message.error("请先选择需要查看的数据");
      }
    },

    // 获取当前日期
    getData() {
      var time = new Date();
      const y = time.getFullYear();
      const m = (time.getMonth() + 1 + "").padStart(2, "0");
      const d = (time.getDate() + "").padStart(2, "0");
      this.timeYMD = `${y}-${m}-${d}`;
    },
    // 点击当前
    rowChange(val) {
      this.row = val;
      this.fId = val.fId;
    },
    rowChange_dialog(val) {
      this.row_dialog = val;
      this.fId_dialog = val.fId;
    },
    // 刪除
    delteFile(file) {
      for (let index = 0; index < this.fSourcefileList.length; index++) {
        if (file.uid == this.fSourcefileList[index].uid) {
          if (this.fSourcefileList[index].atta == undefined) {
            this.recordfiles.push({
              fId: this.fSourcefileList[index].fId,
            });
          } else {
            this.recordfiles.push({
              fId: this.fSourcefileList[index].atta.fId,
            });
          }
          this.fSourcefileList.splice(index, 1);
        }
      }
    },
    // 多选方法
    selectionChange() {},
    // 排序方法
    sortChange() {},
  },
};
</script>

<style scoped>
.printTables {
  background: #fff;
  display: flex;
  justify-content: end;
  padding: 5px;
  margin-top: 10px;
}

.dialogBody {
  padding: 3px 20px;
  height: 80%;
  overflow-y: auto;
}

.baseTitle {
  /* border: 1px solid #0c204d; */
  border-bottom: 1px solid #0c204d;
  height: 30px;
  line-height: 30px;
  position: relative;
  padding-left: 15px;
}

.baseTitle::after {
  display: block;
  content: "";
  height: 30px;
  width: 5px;
  position: absolute;
  left: 0;
  top: 0;
  background: #0c204d;
}
/* 弹框样式 */
.commonBigBox {
  width: 100%;
  height: 100%;
  display: flex;
}
.commonBox {
  width: 50%;
  height: 100%;
}
/* 弹框样式 */
</style>